package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.VocBookConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocCategoryMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocBookDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocBookCommand;
import com.cskaoyan.wordmemorize.request.VocBookPageRequest;
import com.cskaoyan.wordmemorize.service.VocBookService;
import com.cskaoyan.wordmemorize.service.VocCategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VocBookServiceImpl implements VocBookService {


    @Resource
    VocCategoryMapper vocCategoryMapper;
    @Resource
    VocBookMapper vocBookMapper;
    @Resource
    VocBookConverter vocBookConverter;

    @Override
    public PageDTO<VocBookDTO> getBookPage(VocBookPageRequest pageQuery) {
        PageDTO<VocBookDTO> vocBookDTOPageDTO;
        // 1. 提取出pageQuery里面的条件
        if (pageQuery.getPageNum() != null){
            Page<VocBookDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
            QueryWrapper<VocBookDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(pageQuery.getCategoryId() != null,"bc_id",pageQuery.getCategoryId())
                    .like(pageQuery.getKeyword() != null,"name",pageQuery.getKeyword());
            // 查询出对应的数据 ，并转换为对应的PageDTO
            IPage<VocBookDO> vocBookDOIPage = vocBookMapper.selectPage(page, queryWrapper);
            List<VocBookDO> records = vocBookDOIPage.getRecords();
            vocBookDTOPageDTO = vocBookConverter.vocBookList2Page(records, vocBookDOIPage.getTotal());

        }else {
            QueryWrapper<VocBookDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("bc_id",pageQuery.getCategoryId());

            // 查询出对应的数据 ，并转换为对应的PageDTO
            List<VocBookDO> vocBookDOS = vocBookMapper.selectList(queryWrapper);
            vocBookDTOPageDTO = vocBookConverter.vocBookList2Page(vocBookDOS, Long.valueOf(vocBookDOS.size()));

        }
        return vocBookDTOPageDTO;
    }

    @Override
    @Transactional
    public Long addVocBook(VocBookCommand command) {

        // 1. id 没有，则添加一个，有就继续
        if (command.getId() == null){
            command.setId(null);
        }

        // 2. 转换为相应的DO对象
        VocBookDO vocBookDO = vocBookConverter.vocBookCommand2VocBookDO(command);
        int insert = vocBookMapper.insert(vocBookDO);
        UpdateWrapper<VocCategoryDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",vocBookDO.getBcId())
                .setSql("book_count = book_count + 1" );
        vocCategoryMapper.update(null, updateWrapper);
        if (insert > 0){
            return 1L;
        }
        return 0L;
    }

    @Override
    @Transactional
    public void updateVocBook(VocBookCommand command) {
        VocBookDO vocBookDO = vocBookConverter.vocBookCommand2VocBookDO(command);
        vocBookMapper.updateById(vocBookDO);
    }

    @Override
    @Transactional
    public DeleteBookOperateDTO deleteVocBook(Long bookId) {
        VocBookDO vocBookDO = vocBookMapper.selectById(bookId);
        vocBookMapper.deleteById(bookId);
        DeleteBookOperateDTO deleteBookOperateDTO = vocBookConverter.vocBookDO2DeleteBookDTO(vocBookDO);
        return deleteBookOperateDTO;
    }

    @Override
    @Transactional
    public List<DeleteBookOperateDTO> deleteVocBooks(BatchDeleteCommand command) {
        List<String> ids = command.getIds();
        List<VocBookDO> vocBookDOS = vocBookMapper.selectByIds(ids);
        vocBookMapper.deleteByIds(ids);
        List<DeleteBookOperateDTO> deleteBookOperateDTOS = vocBookConverter.vocBookDOs2DeleteBookDTOs(vocBookDOS);
        return deleteBookOperateDTOS;
    }
}
